package com.controller;

import com.business.hexinpass.util.Validator;
import com.entity.Menu;
import com.entity.Role;
import com.entity.RoleMenu;
import com.service.MenuService;
import com.service.RoleMenuService;
import com.service.RoleService;
import com.vo.JsonReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by jsb-160 on 2018/7/25.
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    RoleMenuService roleMenuService;

    /**
     * 添加或修改角色
     */
    @RequestMapping(value = "/addOrEditRole",method = RequestMethod.POST)
    @ResponseBody
    public JsonReturn addOrEditRole(Role role){
        JsonReturn jsonReturn=new JsonReturn();
//        添加角色
        try {
            if(Validator.isNull(String.valueOf(role.getId()))&&Validator.isNotNull(role.getName())){
               Integer ret = roleService.insertRole(role);
                if(Validator.isNotNull(String.valueOf(ret))){
                    jsonReturn.setSuccess(true);
                    jsonReturn.setData("添加成功");
                }
                else
                {
                    jsonReturn.setSuccess(false);
                    jsonReturn.setErrorCode(102);
                    jsonReturn.setError("添加失败");
                }
            }
    //        修改角色
            else {
                Integer ret = roleService.updateRole(role);
                if(Validator.isNotNull(String.valueOf(ret))){
                    jsonReturn.setSuccess(true);
                    jsonReturn.setErrorCode(100);
                    jsonReturn.setData("更新成功");
                }
                else
                {
                    jsonReturn.setSuccess(false);
                    jsonReturn.setErrorCode(102);
                    jsonReturn.setError("更新失败");
                }
            }
        } catch (Exception e) {
            jsonReturn.setSuccess(false);
            jsonReturn.setErrorCode(108);
            jsonReturn.setData("系统错误，请稍后再试！");
            e.printStackTrace();
        }
        return jsonReturn;
    }

    /***
     * 删除角色
     */
    @RequestMapping(value = "/deleteRoleById")
    @ResponseBody
    public JsonReturn deleteRole(Integer id){
        JsonReturn jsonReturn=new JsonReturn();
        try {
            if(Validator.isNotNull(String.valueOf(id))){
               Integer ret= roleService.deleteRoleById(id);
                if(Validator.isNotNull(String.valueOf(ret))){
                    jsonReturn.setSuccess(true);
                    jsonReturn.setErrorCode(102);
                    jsonReturn.setData("删除成功");
                }
                else
                {
                    jsonReturn.setSuccess(false);
                    jsonReturn.setErrorCode(102);
                    jsonReturn.setError("删除失败");
                }
            }
        } catch (Exception e) {
            jsonReturn.setSuccess(false);
            jsonReturn.setErrorCode(108);
            jsonReturn.setData("系统错误，请稍后再试！");
            e.printStackTrace();
        }
        return jsonReturn;
    }

/**
 * 分配菜单
 */
@RequestMapping(value = "addMenu")
@ResponseBody
    public JsonReturn addMenu(String roleId,String menuIds){
        JsonReturn jsonReturn=new JsonReturn();
        try {
            if(Validator.isNotNull(roleId)){
                String[] ids=menuIds.split(",");
                RoleMenu roleMenu=new RoleMenu();
                roleMenu.setRoleId(Integer.valueOf(roleId));
                if(ids.length>0){
                    for(int i=0;i<ids.length;i++){
                        roleMenu.setMenuId(Integer.valueOf(ids[i]));
                        roleMenuService.addRoleMenu(roleMenu);
                    }
                }
                jsonReturn.setSuccess(true);
                jsonReturn.setErrorCode(100);
                jsonReturn.setData("分配成功");
            }
            else {
                jsonReturn.setSuccess(false);
                jsonReturn.setErrorCode(102);
                jsonReturn.setData("角色获取失败");
            }
        } catch (NumberFormatException e) {
            jsonReturn.setSuccess(false);
            jsonReturn.setErrorCode(108);
            jsonReturn.setData("系统错误，请稍后再试！");
            e.printStackTrace();
        }

        return jsonReturn;
    }

}
